package com.example.zzl.adapter;

import android.content.Context;
import android.databinding.ViewDataBinding;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by Administrator on 2017/1/7.
 */

public class BasePagerAdapter<T> extends PagerAdapter {

    protected Context context;
    protected List<T> list;
    protected LayoutInflater inflater;
    protected ViewDataBinding[] datas;

    public BasePagerAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;;
        inflater = LayoutInflater.from(context);
        datas = new ViewDataBinding[list.size()];
    }

    @Override
    public void notifyDataSetChanged() {
        if (datas.length != list.size()) {
            datas = new ViewDataBinding[list.size()];
        }
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(datas[position].getRoot());
    }
}
